<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="方案名称" prop="schemeName">
        <el-input v-model="queryParams.schemeName" placeholder="请输入方案名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="schemeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="方案ID" align="center" prop="schemeId" />
      <el-table-column label="方案名称" align="center" prop="schemeName" />
      <el-table-column label="图片" align="center" prop="imageUrl">
        <template #default="scope">
          <el-image v-if="scope.row.imageUrl" :src="scope.row.imageUrl" style="width: 60px; height: 60px" fit="cover" />
        </template>
      </el-table-column>
      <el-table-column label="简要说明" align="center" prop="description" />
      <el-table-column label="外部ID" align="center" prop="externalId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改方案对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="schemeRef" label-width="80px">
        <el-form-item label="方案名称" prop="schemeName">
          <el-input v-model="form.schemeName" placeholder="请输入方案名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="简要说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入简要说明" maxlength="255" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入图片URL或上传图片" maxlength="255" />
        </el-form-item>
        <el-form-item label="外部ID" prop="externalId">
          <el-input v-model="form.externalId" placeholder="请输入外部系统ID" maxlength="50" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="255" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Scheme">
import { listScheme, getScheme, addScheme, updateScheme, delScheme } from '@/api/geek/scheme'
import { getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance()

const schemeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    schemeName: undefined
  },
  rules: {
    schemeName: [
      { required: true, message: '方案名称不能为空', trigger: 'blur' },
      { min: 2, max: 50, message: '方案名称长度必须介于 2 和 50 之间', trigger: 'blur' }
    ]
  }
})
const { queryParams, form, rules } = toRefs(data)

function getList() {
  loading.value = true
  listScheme(queryParams.value).then(res => {
    schemeList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.schemeId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  queryParams.value.schemeName = undefined
  getList()
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '添加检测方案'
}

function handleUpdate(row) {
  reset()
  const schemeId = row?.schemeId || ids.value[0]
  getScheme(schemeId).then(res => {
    form.value = res.data
    open.value = true
    title.value = '修改检测方案'
  })
}

function handleDelete(row) {
  const schemeIds = row?.schemeId ? [row.schemeId] : ids.value
  if (!schemeIds.length) return
  proxy.$modal.confirm('是否确认删除选中的检测方案？', '提示').then(() => {
    delScheme(schemeIds.join(',')).then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
  }).catch(() => {})
}

function submitForm() {
  const isEdit = !!form.value.schemeId
  const checkUnique = schemeList.value.every(item =>
    item.schemeName !== form.value.schemeName || item.schemeId === form.value.schemeId
  )
  if (!checkUnique) {
    proxy.$modal.error('方案名称已存在')
    return
  }
  const action = isEdit ? updateScheme : addScheme
  action(form.value).then(() => {
    proxy.$modal.msgSuccess(isEdit ? '修改成功' : '新增成功')
    open.value = false
    getList()
  })
}

function reset() {
  form.value = {
    schemeId: undefined,
    schemeName: undefined,
    description: undefined,
    imageUrl: undefined,
    externalId: undefined,
    remark: undefined
  }
}

function cancel() {
  open.value = false
  reset()
}

getList()
</script> 